<?php
namespace Admin\Controller;
use Think\Controller;
class FenleiController extends CommonController {
     public function lists(){
		$fenlei = M("fenlei");
		$list = $fenlei->query("select * from zwp_fenlei as a order by concat(a.pid,a.id)");
		foreach($list as $k=>$v){
			$count = substr_count($v['pid'],",");
			$list[$k]['order']=$count;
			$list[$k]["name"]= $this->kong($count).$v['name'];
		}
		$this->assign("lists",$list);
		$this->display();
	}
	public function kong($a){
		
		$str = "";
		for($i=0;$i<$a;$i++){
			
			$str .="&nbsp;---|";
			
		}
		return $str;
	}
	public function add(){
		$fenlei = M("fenlei");
		$sql = $fenlei->query("select * from zwp_fenlei as a order by concat(a.pid,a.id)");
		$this->assign("sql",$sql);
		$this->display();
	}
	public function addAction(){
		$sql = M("fenlei")->add($_POST);
		//判断
		if($sql){
			$this->success("添加成功");
		}else{
			$this->error("添加失败");
		}
	}
	public function del(){
		$id = $_GET["uid"];
		//判断它有没有子集
		$sql = M("fenlei")->where("id= '$id'")->select();
		//取得pid和id结合的内容
		$str = $sql[0]['pid'].$sql[0]['id'].",";
		//查询子集是否存在
		$sql_zi = M("fenlei")->where("pid = '$str'")->select();
		if(empty($sql_zi)){
			//为空时能进行删除
			$sql = M("fenlei")->where("id=$id")->delete();
			$this->success("删除成功");
		}else{
			//不为空是不能删除
			$this->error("有子集不能进行删除");
		}
	}
	public function edit(){
		$id = $_GET["uid"];
		$sql = M("fenlei")->where("id=$id")->find();//查询一条数据
		$this->assign("sql",$sql);
		$this->display();
		
	}
	public function editAction(){
		$id = $_POST["id"];
		$sql = M("fenlei"); // 实例化User对象
		$info = $sql->where("id=$id")->save($_POST); // 根据条件更新记录
		if($info){
			//成功
			$this->success("修改成功");
		}else{
			//失败
			$this->error("修改失败");
			
		}
		
	}
}